package com.example;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ClassDAO {
    public List<Class> getAllClasses() throws SQLException {
        List<Class> classes = new ArrayList<>();
        String sql = "SELECT * FROM classes ORDER BY name";
        
        try (Connection conn = DatabaseUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                classes.add(new Class(
                    rs.getInt("id"),
                    rs.getString("name"),
                    rs.getInt("major_id"),
                    rs.getString("grade"),
                    rs.getString("created_at")
                ));
            }
        }
        return classes;
    }

    public List<Class> getClassesByMajor(int majorId) throws SQLException {
        List<Class> classes = new ArrayList<>();
        String sql = "SELECT * FROM classes WHERE major_id = ? ORDER BY name";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, majorId);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                classes.add(new Class(
                    rs.getInt("id"),
                    rs.getString("name"),
                    rs.getInt("major_id"),
                    rs.getString("grade"),
                    rs.getString("created_at")
                ));
            }
        }
        return classes;
    }

    public void addClass(Class class_) throws SQLException {
        String sql = "INSERT INTO classes (name, major_id, grade) VALUES (?, ?, ?)";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, class_.getName());
            pstmt.setInt(2, class_.getMajorId());
            pstmt.setString(3, class_.getGrade());
            pstmt.executeUpdate();
        }
    }

    public void updateClass(Class class_) throws SQLException {
        String sql = "UPDATE classes SET name = ?, major_id = ?, grade = ? WHERE id = ?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, class_.getName());
            pstmt.setInt(2, class_.getMajorId());
            pstmt.setString(3, class_.getGrade());
            pstmt.setInt(4, class_.getId());
            pstmt.executeUpdate();
        }
    }

    public void deleteClass(int id) throws SQLException {
        String sql = "DELETE FROM classes WHERE id = ?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
        }
    }
} 